package com.wawayaya.bookreader.bean;

import com.wawayaya.bookreader.DefaultSettings;
import java.io.File;
import java.io.Serializable;


public class Bookpartstatus implements Serializable {

	private static final long serialVersionUID = 1L;

	private onepartstatus[] m_parts = null;
	private int m_inowprogress = 0;

	public Bookpartstatus(StringQuadruple[] sparturls) {
		m_parts = new onepartstatus[sparturls.length];
		for(int i = 0; i < sparturls.length; i++) {
			m_parts[i] = new onepartstatus(sparturls[i].m_sVal1, i + 1);
		}
	}
	
	public void setprogressbeginend(int ipartnummber, int iprogressbegin, int iprogressend) {
		if(ipartnummber > 0 && ipartnummber <= m_parts.length && null != m_parts[ipartnummber - 1]) {
			 m_parts[ipartnummber - 1].m_iprogressbegin = iprogressbegin;
			 m_parts[ipartnummber - 1].m_iprogressend = iprogressend;
		}
	}
	
	public void setbookhasbeendownloadprogress(int iprogress) {
		m_inowprogress = iprogress;
	}
	
	public void setpartimagescombinetype(int ipartnumber, int itype){
		if(ipartnumber > 0 && ipartnumber <= m_parts.length && null != m_parts[ipartnumber - 1]) {
			 m_parts[ipartnumber - 1].m_icombinetype = itype;
		}
	}
	
	public void setpartimagescombinetypeOk(int ipartnumber, boolean bisok){
		if(ipartnumber > 0 && null != m_parts && ipartnumber <= m_parts.length && null != m_parts[ipartnumber - 1]) {
			 m_parts[ipartnumber - 1].m_bcombineok = bisok;
		}
		int iokcount = 0;
		try{		
			for(int i = 0; i < m_parts.length; i++) {
				if(m_parts[i].m_bcombineok) {
					iokcount++;
				}
			}	
			if(iokcount == m_parts.length) {
				m_inowprogress = 100;
			}
			else if(m_parts.length > 0){
				m_inowprogress = iokcount * 100 / m_parts.length;
			}
		}catch(Exception e){
		}
	}
	
	public IntDouble getpartrangepercent(int ipartnumber){
		if(ipartnumber > 0 && ipartnumber <= m_parts.length && null != m_parts[ipartnumber - 1]) {
			 return new IntDouble(m_parts[ipartnumber - 1].m_iprogressbegin, m_parts[ipartnumber - 1].m_iprogressend);
		}
		return null;
	}
	
	public int getpartimagescombinetype(int ipartnumber){
		if(ipartnumber > 0 && ipartnumber <= m_parts.length && null != m_parts[ipartnumber - 1]) {
			return m_parts[ipartnumber - 1].m_icombinetype;
		}
		return DefaultSettings.ms_Json_ImgCombineType_unknown;
	}
	
	public int getbookhasbeendownloadprogress(){
		return m_inowprogress;
	}
	
	public void setbookdownloadedfalse() {
		m_inowprogress = 0;
		for(int i = 0; i < m_parts.length; i++) {
			if(null != m_parts[i]) {
				m_parts[i].m_bcombineok = false;
			}
		}
	}	
	
	public boolean bookpart1hasbeendownloaded(String sserpath) {
		try{
			if(null != m_parts && m_parts.length > 0 && null != m_parts[0]) {
				if(m_parts[0].m_bcombineok) {
					return new File(sserpath).exists();
				}
			}
		}catch(Exception e){
		}
		return false;
	}
	
	public void resetexistedstatus() {
		if(null != m_parts) {
			for(int i = 0; i < m_parts.length; i++) {
				if(null != m_parts[i]) {
					m_parts[i].resetexistedstatus();
				}
			}
		}
		m_inowprogress = 0;
	}
	
	/* ======================= */
	
	private class onepartstatus implements Serializable {

		private static final long serialVersionUID = 1L;
		
		public onepartstatus(String sparturl, int ipartnumber) {
			m_sparturl = sparturl;
			m_ipartnumber = ipartnumber;
		}
		
		void resetexistedstatus() {
			m_icombinetype = DefaultSettings.ms_Json_ImgCombineType_unknown;
			m_bcombineok = false;
		}
		
		String m_sparturl = null;
		// m_ipartnumber starts from 1
		int m_ipartnumber = 1; 
		int m_iprogressbegin = 0;
		int m_iprogressend = 0;
		int m_icombinetype = DefaultSettings.ms_Json_ImgCombineType_unknown;
		boolean m_bcombineok = false;
	}
}
